/**
 *
 * @file options.h
 *
 * @brief Command line parser header
 *
 * $Id: options.h 116 2009-01-15 07:23:52Z henri.doreau $
 */

/*
 * This file is part of Tsar.
 *
 * Tsar is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tsar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tsar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef OPTIONS_H
#define OPTIONS_H


/* -- S T A N D A R D  I N C L U D E S -- */
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>


/* -- C O M M A N D  L I N E  P A R S I N G  I N C L U D E -- */
#include <getopt.h>


/* -- L O C A L  I N C L U D E S -- */
#include <utils.h>


/* -- C O N S T A N T S -- */
#if defined( __linux__)
  /** @def Default network interface name, according to common ones for the system tsar is compiled on */
  #define DEFAULT_NETWORK_INTERFACE "eth0"
#elif defined( __FreeBSD__)
  /** @def Default network interface name, according to common ones for the system tsar is compiled on */
  #define DEFAULT_NETWORK_INTERFACE "nfe0"
#else
  /** @def Default network interface name, if no system is recognized : then set to any and the user or libpcap will do the job... */
  #define DEFAULT_NETWORK_INTERFACE "any"
#endif

/** @def Default name for logs */
#define     DEFAULT_FILENAME                NULL

/** @def Default value (number of milliseconds) after what we refresh the dump file */
#define     DEFAULT_RCV_TIMEOUT             1000

/** @def Default verbosity level (out of 5, from 0 to 4) */
#define     DEFAULT_VERBOSITY_LEVEL         0

/** @def Default capture state */
#define     DEFAULT_PROMISC_MODE_ENABLED    false

/** @def port to bind the built-in http server to */
#define     DEFAULT_HTTP_SERVER_PORT        80

/** @def default state for the sniffer at launch time, disabled require a pcap file to load  */
#define     DEFAULT_START_SNIFFER           true

/** @def default filename for the output file where to store captured traffic */
#define     DEFAULT_PCAP_FILENAME           "tsar_dump_traffic.pcap"

#define     DEFAULT_SNIFFER_FILTER          ""


/* -- M A C R O S -- */
/** @def returns a string according to a boolean variable's value */
#define BOOL_STATE( val )   ( (val) ? "Enabled" : "Disabled" )

/** @def set options to their default values */
#define SET_OPTIONS_DEFAULT( o )                                        \
{                                                                       \
    /* A zero terminated string according to your operating             \ 
       system's most common network interface */                        \
    o->iface            =   DEFAULT_NETWORK_INTERFACE;                  \
    /* Default filename to create reports */                            \
    o->outputFile       =   DEFAULT_FILENAME;                           \
    /* Refresh dump file every XX seconds */                            \
    o->timeoutMs        =   DEFAULT_RCV_TIMEOUT;                        \
    /* Port to bind the built-in http server to */                      \
    o->httpServerPort   =   DEFAULT_HTTP_SERVER_PORT;                   \
    /* Load a local PCAP file instead of launching the sniffer */       \
    o->startSniffer     =   DEFAULT_START_SNIFFER;                      \
    o->pcapFile         =   DEFAULT_PCAP_FILENAME;                      \
    /* Apply a first bpf filter on the sniffer */                       \
    o->snifferFilter    =   DEFAULT_SNIFFER_FILTER;                     \
    /* Are you really wondering what two following lines are about?? */ \
    o->verbosityLevel   =   DEFAULT_VERBOSITY_LEVEL;                    \
    o->debug            =   false;                                      \
    /* Promiscuous mode <==> capture every packet we can,               \
       not only the one destined to us */                               \
    o->promisc          =   DEFAULT_PROMISC_MODE_ENABLED;               \
}



/* -- T Y P E D E F S -- */
/** @brief store user's choices and settings during a session */
typedef struct {
    /** @brief network interface to monitor */
    char * iface;
    /** @brief duplicate output streams to this file */
    char * outputFile;
    /** @brief recv timeout in milliseconds */
    unsigned long timeoutMs;
    /** @brief port onto which we'll bind the built-in http server */
    unsigned short httpServerPort;
    /* @brief pcap file to load instead of launching sniffer to build one */
    bool_t startSniffer;
    char * pcapFile;
    char * snifferFilter;
    /** @brief how much do we have to report our activities */
    unsigned char verbosityLevel;
    /** @brief debug : full verbose */
    bool_t debug;
    /** @brief Should we accept every packet we can? */
    bool_t promisc;
} opt_t;


/* -- F U N C T I O N S  P R O T O T Y P E S -- */
/**
 * @brief parse command line and extract options
 * @param argc number of argument passed to main()
 * @param argv array of these arguments
 * @param o opt_t structure where to store options after they are parsed
 * @return error code according to the execution of the function
 * @retval 0 no error encoutered. We can continue 
 * @retval >0 an invalid argument supplied
 */
int tsar_get_options( int argc, char **argv, opt_t * o );

/**
 * @brief print out every option and selected value for this session
 * @param o points to the structure where options are stored
 * @param forceTraceOpt increase the priority of this printing to the maximum to ensure output
 */
void trace_options( opt_t * o, bool_t forceTraceOpt );

#endif /* OPTIONS_H */

